package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.*;
import com.wzq.service.IMessageService;
import com.wzq.service.IProjectService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/project")
public class AdminProjectController extends GetSome {

    @Autowired
    IProjectService iProjectService;

//    @RequestMapping("/list")
//    public R list(@RequestBody PageBean pageBean) {
//        System.out.println(pageBean);
//        String query = pageBean.getQuery().trim();
//        Page<Project> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
//        Page<Project> pageResult = iProjectService.page(page, new QueryWrapper<Project>().like(StringUtil.isNotEmpty(query), "title", query));
//        //        添加用户名称和项目名称
//        List<Project> list = new ArrayList<>();
//        for (Project project : pageResult.getRecords()) {
//            if (project.getIstop() == 1) project.setTop_c("置顶");
//            else project.setTop_c("未置顶");
//            if (project.getApply() == 1) project.setApply_c("允许加入");
//            else project.setApply_c("不允许");
//            if (project.getIsover() == 1) project.setOver_c("结束");
//            else project.setOver_c("未结束");
//            project.setT_type(getT_type(project.getType()));
//            list.add(project);
//        }
//        Map<String, Object> map = new HashMap<>();
//        map.put("ProjectList", pageResult.getRecords());
//        map.put("total", pageResult.getTotal());
//
//        return R.ok(map);
//    }
//
    @RequestMapping("/list/resources")

    public R Resourceslist(@RequestBody PageBean pageBean) {
        System.out.println(pageBean);
        String query = pageBean.getQuery().trim();
        Page<Project> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<Project> pageResult = iProjectService.page(page, new QueryWrapper<Project>().like(StringUtil.isNotEmpty(query), "title", query));
        //        添加用户名称和项目名称
        List<Project> list = new ArrayList<>();
        Image image;
        LambdaQueryWrapper<Image> getImg = new LambdaQueryWrapper<>();
        for (Project project : pageResult.getRecords()) {
            if(getparent(project.getType())==0) {
                if (project.getIstop() == 1) project.setTop_c("置顶");
                else project.setTop_c("未置顶");
                if (project.getApply() == 1) project.setApply_c("允许加入");
                else project.setApply_c("不允许");
                if (project.getIsover() == 1) project.setOver_c("结束");
                else project.setOver_c("未结束");
                project.setT_type(getT_type(project.getType()));
                // 封装图片路径
                getImg.clear();
                getImg.eq(Image::getP_id,project.getId()).eq(Image::getType,1);
                image=iImageService.getOne(getImg);
                project.setPicurl(image==null?"":image.getUrl());
                list.add(project);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ProjectList",list);
        map.put("total", list.size());

        return R.ok(map);
    }

    @RequestMapping("/list/activity")
    public R Activitylist(@RequestBody PageBean pageBean) {
        System.out.println(pageBean);
        String query = pageBean.getQuery().trim();
        Image image;
        LambdaQueryWrapper<Image> getImg = new LambdaQueryWrapper<>();
        Page<Project> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<Project> pageResult = iProjectService.page(page,
                new QueryWrapper<Project>().like(StringUtil.isNotEmpty(query), "title", query));
        //        添加用户名称和项目名称
        List<Project> list = new ArrayList<>();
        for (Project project : pageResult.getRecords()) {
            if (getparent(project.getType())==1) {
                if (project.getIstop() == 1) project.setTop_c("置顶");
                else project.setTop_c("未置顶");
                if (project.getApply() == 1) project.setApply_c("允许加入");
                else project.setApply_c("不允许");
                if (project.getIsover() == 1) project.setOver_c("结束");
                else project.setOver_c("未结束");
                project.setT_type(getT_type(project.getType()));
                // 封装图片路径
                getImg.clear();
                getImg.eq(Image::getP_id,project.getId()).eq(Image::getType,1);
                image=iImageService.getOne(getImg);
                project.setPicurl(image==null?"":image.getUrl());
                list.add(project);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ProjectList", list);
        map.put("total", list.size());

        return R.ok(map);
    }

    @RequestMapping("/save")
    public R save(@RequestBody Project project) {
        if (project.getId() == null || project.getId() == -1) {
            iProjectService.save(project);
        } else {
            Project old_p = iProjectService.getById(project.getId());
            iProjectService.saveOrUpdate(project);
            if (!old_p.getIstop().equals(project.getIstop()) && project.getAuthor() != 0) {
                Message message = new Message();
                if (project.getIstop() == 1)
                    message.setType(7);         //置顶
                else
                    message.setType(8);         //置顶

                message.setW_id_down(project.getAuthor());
                message.setP_id(project.getId());
                message.setName("管理员");
                iMessageService.save(message);
            }
        }
        return R.ok();
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id") Integer id) {
        Project project = iProjectService.getById(id);
        //        获得项目名称

        project.setT_type(getT_type(project.getType()));
        Map<String, Object> map = new HashMap<>();
        map.put("project", project);
        return R.ok(map);
    }

//    @GetMapping("/{id}")            //单个回显
//    public R findById(@PathVariable(value = "id")Integer id){
//        Project project= iProjectService.getById(id);
//        //        获得用户名称和项目名称
//        Map<String,Object> map=new HashMap<>();
//        map.put("project",project);
//        return R.ok(map);
//    }


    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id") Integer id) {
        iProjectService.removeById(id);
        return R.ok();
    }

    @RequestMapping("/listAll")
    public R listAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("projectList", iProjectService.list());
        return R.ok(map);
    }

    @RequestMapping("/listAll1")
    public R listAll1(){
        QueryWrapper queryWrapper=new QueryWrapper<Type>();
        queryWrapper.eq("type",7);
        Map<String,Object> map=new HashMap<>();
        map.put("projectList1",iProjectService.list(queryWrapper));
        return R.ok(map);
    }

}

